<?php

//echo $_POST["who"];

$select = "select * from podzakazka where cislo_objednavky = '".urldecode($_GET["updatepz"])."'";
if (Is_Array($pole_pz = DbSelect($select)))
{
  $pz_id = $pole_pz["id"];
}

            // ulozeni do DB
$d = strtok($_POST["datum_instalace"], '/');
$m = strtok('/');
$r = strtok('/');
$datum_instalace = "$r-$m-$d 00:00:00";
$_POST["datum_instalace"] = $datum_instalace;
$d = strtok($_POST["dorazi_kdy"], '/');
$m = strtok('/');
$r = strtok('/');
$dorazi_kdy = "$r-$m-$d 00:00:00";
$_POST["dorazi_kdy"] = $dorazi_kdy;
$d = strtok($_POST["datum_nahledu"], '/');
$m = strtok('/');
$r = strtok('/');
$datum_nahledu = "$r-$m-$d 00:00:00";
$_POST["datum_nahledu"] = $datum_nahledu;

$uz_je = false;
$update = "update podzakazka set ";

function helper($pole, $co)
{
  if ($pole[$co] != $_POST[$co])
  {
    if ($GLOBALS["uz_je"])
    {
      $GLOBALS["update"].=" , ";
    }
    $GLOBALS["uz_je"]=true;
    $GLOBALS["update"].= " $co = '".$_POST[$co]."' ";

    if (isset($_GET["addz"]) || isset($_GET["addpz"]))
    {
      //echo $co."<br>";
    }
    else
    {
      $id = vrat_max_id("dbupdate") + 1;
      $insert = "insert into dbupdate values ($id, '".$_POST["who"]."', '".urldecode($_GET["updatepz"])."', '$co', '".$pole[$co]."', '".$_POST[$co]."', 0, '', CURRENT_TIMESTAMP())";
      DbQuery($insert, $_POST["who"]);
    }
  }
}

helper($pole_pz, "podklady");
helper($pole_pz, "se_zakazkou");
helper($pole_pz, "dorazi_kdy");
helper($pole_pz, "nahled");
helper($pole_pz, "datum_nahledu");
helper($pole_pz, "technologie");
helper($pole_pz, "material");
helper($pole_pz, "barevnost_lic");
helper($pole_pz, "barevnost_rub");
helper($pole_pz, "tisk");
helper($pole_pz, "barevnost_poznamka");
//helper($pole_pz, "metru_ctverecnich");

helper($pole_pz, "lamino_lic");
helper($pole_pz, "lamino_rub");
helper($pole_pz, "lamino_poznamka");
helper($pole_pz, "kasir_lic");
helper($pole_pz, "kasir_rub");
helper($pole_pz, "kasir_poznamka");
helper($pole_pz, "specialni_zpracovani");
helper($pole_pz, "orez_kniharna");
helper($pole_pz, "baleni");
helper($pole_pz, "instalace");
helper($pole_pz, "datum_instalace");
helper($pole_pz, "instalace_montaz");
helper($pole_pz, "poznamka_vyrobni");
helper($pole_pz, "sortiment");
helper($pole_pz, "naklad_celkem");
helper($pole_pz, "prijem_celkem");


/*
$m2 = sprintf("%.0f", $_POST["metru_ctverecnich"]);
$m2_2 = sprintf("%.0f", $pole_pz["metru_ctverecnich"]);
if ($m2_2 != $m2)
{
  if ($uz_je)
  {
    $update .=" , ";
  }
  $uz_je=true;
  $update .= " metru_ctverecnich = '$m2' ";
  $id = vrat_max_id("dbupdate") + 1;
  $insert = "insert into dbupdate values ($id, '".$_POST["who"]."', '".urldecode($_GET["updatepz"])."', 'metru_ctverecnich', '$m2_2', '$m2', 0, '', CURRENT_TIMESTAMP())";
  DbQuery($insert, $_POST["who"]);
}
*/

$update .= " where cislo_objednavky = '".urldecode($_GET["updatepz"])."'";
//echo $update;

DbQuery($update, $_POST["who"]);

$i = 1;
// najit format_id
$select = "select * from format where pz_id = ".$pole_pz["id"];
//echo $select;
$result = GetDbResult($select);
if ($result != -1)
{
  while ($pole_f = GetDbFArray($result))
  {
    $uz_je = false;
    $je_format = false;
    $_POST["sirka_$i"] = str_replace(",", ".", $_POST["sirka_$i"]);
    $_POST["vyska_$i"] = str_replace(",", ".", $_POST["vyska_$i"]);
    $_POST["m2_$i"] = str_replace(",", ".", $_POST["m2_$i"]);
    $_POST["pocet_kusu_$i"] = str_replace(",", ".", $_POST["pocet_kusu_$i"]);
    $_POST["naklad_$i"] = str_replace(",", ".", $_POST["naklad_$i"]);
    $_POST["prijem_$i"] = str_replace(",", ".", $_POST["prijem_$i"]);

    $update = "update format set ";
    if ($pole_f["motiv"] != $_POST["motiv_$i"])
    {
      if ($uz_je) {$update.=" , ";} $uz_je = true; $update .= " motiv = '".$_POST["motiv_$i"]."'";
      $id = vrat_max_id("dbupdate") + 1;
      $insert = "insert into dbupdate values ($id, '".$_POST["who"]."', '".urldecode($_GET["updatepz"])."', 'motiv', '".$pole_f["motiv"]."', '".$_POST["motiv_$i"]."', 0, '', CURRENT_TIMESTAMP())";
      DbQuery($insert, $_POST["who"]);
    }
    if ($pole_f["sirka"] != $_POST["sirka_$i"])
    {
      if ($uz_je) {$update.=" , ";} $je_format = true; $uz_je = true; $update .= " sirka = '".$_POST["sirka_$i"]."'";
    }
    if ($pole_f["vyska"] != $_POST["vyska_$i"])
    {
      if ($uz_je) {$update.=" , ";} $je_format = true; $uz_je = true; $update .= " vyska = '".$_POST["vyska_$i"]."'";
    }
    if ($pole_f["pocet_kusu"] != $_POST["pocet_kusu_$i"])
    {
      if ($uz_je) {$update.=" , ";} $je_format = true; $uz_je = true; $update .= " pocet_kusu = '".$_POST["pocet_kusu_$i"]."'";
    }
    if ($pole_f["pocet_visualu"] != $_POST["pocet_visualu_$i"])
    {
      if ($uz_je) {$update.=" , ";} $uz_je = true; $update .= " pocet_visualu = '".$_POST["pocet_visualu_$i"]."'";
      $id = vrat_max_id("dbupdate") + 1;
      $insert = "insert into dbupdate values ($id, '".$_POST["who"]."', '".urldecode($_GET["updatepz"])."', 'pocet_visualu', '".$pole_f["pocet_visualu"]."', '".$_POST["pocet_visualu_$i"]."', 0, '', CURRENT_TIMESTAMP())";
      DbQuery($insert, $_POST["who"]);
    }
    if ($pole_f["m2"] != $_POST["m2_$i"])
    {
      if ($uz_je) {$update.=" , ";} $je_format = true; $uz_je = true; $update .= " m2 = '".$_POST["m2_$i"]."'";
    }
    if ($pole_f["naklad"] != $_POST["naklad_$i"])
    {
      if ($uz_je) {$update.=" , ";} $uz_je = true; $update .= " naklad = '".$_POST["naklad_$i"]."'";
      $id = vrat_max_id("dbupdate") + 1;
      $insert = "insert into dbupdate values ($id, '".$_POST["who"]."', '".urldecode($_GET["updatepz"])."', 'naklad', '".$pole_f["naklad"]."', '".$_POST["naklad_$i"]."', 0, '', CURRENT_TIMESTAMP())";
      DbQuery($insert, $_POST["who"]);
    }
    if ($pole_f["prijem"] != $_POST["prijem_$i"])
    {
      if ($uz_je) {$update.=" , ";} $uz_je = true; $update .= " prijem = '".$_POST["prijem_$i"]."'";
      $id = vrat_max_id("dbupdate") + 1;
      $insert = "insert into dbupdate values ($id, '".$_POST["who"]."', '".urldecode($_GET["updatepz"])."', 'prijem', '".$pole_f["prijem"]."', '".$_POST["prijem_$i"]."', 0, '', CURRENT_TIMESTAMP())";
      DbQuery($insert, $_POST["who"]);
    }
    
    if ($je_format == true)
    {
      $id = vrat_max_id("dbupdate") + 1;
      $prev = num2str($pole_f["sirka"])." x ".num2str($pole_f["vyska"])." x ".num2str($pole_f["pocet_kusu"])." = ".num2str($pole_f["m2"]);
      $next = num2str($_POST["sirka_$i"])." x ".num2str($_POST["vyska_$i"])." x ".num2str($_POST["pocet_kusu_$i"])." = ".num2str($_POST["m2_$i"]);
      $insert = "insert into dbupdate values ($id, '".$_POST["who"]."', '".urldecode($_GET["updatepz"])."', 'format', '$prev', '$next', 0, '', CURRENT_TIMESTAMP())";
      DbQuery($insert, $_POST["who"]);
    }
    $update .= " where id = '".$pole_f["id"]."'";
    
    //echo $update;
    
    DbQuery($update, $_POST["who"]);
    $i++;
  }
  $format_id = vrat_max_id("format") + 1;
  $pz_id = $pole_pz["id"];
  while ($i <= 10)
  {
    if (($_POST["sirka_$i"] != "") || ($_POST["vyska_$i"] != "") || ($_POST["pocet_kusu_$i"] != ""))
    {
      $insert = "insert into format values ($format_id, "
                        ." $pz_id, "
                        ." '".$_POST["motiv_$i"]."', "
                        ." '".str_replace(",", ".", $_POST["sirka_$i"])."', "
                        ." '".str_replace(",", ".", $_POST["vyska_$i"])."', "
                        ." '".str_replace(",", ".", $_POST["pocet_kusu_$i"])."', "
                        ." '".$_POST["pocet_visualu_$i"]."', "
                        ." '".str_replace(",", ".", $_POST["m2_$i"])."', "
                        ." '".str_replace(",", ".", $_POST["naklad_$i"])."', "
                        ." '".str_replace(",", ".", $_POST["prijem_$i"])."')";
      //echo "".$insert."<br>";

      DbQuery($insert, $_POST["who"]);

      if (isset($_GET["addz"]) || isset($_GET["addpz"]))
      {
        //echo $co."<br>";
      }
      else
      {
        $id = vrat_max_id("dbupdate") + 1;
        $insert = "insert into dbupdate values ($id, '".$_POST["who"]."', '".urldecode($_GET["updatepz"])."', 'motiv', '', '".$_POST["motiv"]."', 0, '', CURRENT_TIMESTAMP())";
        DbQuery($insert, $_POST["who"]);
      }
    }
    $i++;
    $format_id++;
  }
}

?>


